#include "Tshm.h"

void show_help(void)
{
	printf("cmd:\n");
	printf("\t#Tshm dumpall (dump all item)\n");
	printf("\t#Tshm wipe (wipe all item)\n");
	printf("\t#Tshm set char xxxx 1 (Set xxxx to 1,type=char)\n");
	printf("\t#Tshm get char xxxx (Get xxxx Value,type=char)\n");
	printf("\t#Tshm set int xxxx 1 (Set xxxx to 1,type=int)\n");
	printf("\t#Tshm get int xxxx (Get xxxx Value,type=int)\n");
	printf("\t#Tshm set str xxxx 1 (Set xxxx to 1,type=str)\n");
	printf("\t#Tshm get str xxxx (Get xxxx Value,type=str)\n");
	printf("\t#Tshm -h (Show help info)\n");
	printf("proc (code) : Your main.c must include \"Tshm.h\" first.Call Tshm_init()\n");
	printf("\tTshm_dumpall() (dump all item)\n");
	printf("\tTshm_wipe() (wipe all item)\n");
	printf("\tTshm_set(int type,char *name,void *value)\n");
	printf("\t type : CHAR_TYPE,INT_TYPE,STR_TYPE\n");
	printf("\t name : Your var name\n");
	printf("\t value : Set Value\n");
	printf("\tTshm_get(int type,char *name,void *value)\n");
	printf("\t type : CHAR_TYPE,INT_TYPE,STR_TYPE\n");
	printf("\t name : Your var name\n");
	printf("\t value : Get Value\n");
}

int main(int argc,char *argv[])
{
	char *item=argv[1];
	char *type=argv[2];
	char *name=argv[3];
	char *setValue=argv[4];
	char cValue;
	int iValue;
	char sValue[TSHM_STR_LEN];
	
	if(item == NULL)
		goto SHOW_HELP;
	
	Tshm_init();
	if(!strcmp(item,"init")){
		
	}
	else if(!strcmp(item,"dumpall")){
		Tshm_dumpall();
	}
	else if(!strcmp(item,"wipe")){
		Tshm_wipe();
	}
	else if(!strcmp(item,"set")){
		if(type == NULL || name == NULL)
			goto SHOW_HELP;
		
		if(!strcmp(type,"char")){
			if(setValue == NULL)
				goto SHOW_HELP;
			cValue = atoi(setValue);
			Tshm_set(CHAR_TYPE,name,&cValue);
		}
		else if(!strcmp(type,"int")){
			if(setValue == NULL)
				goto SHOW_HELP;
			iValue = atoi(setValue);
			Tshm_set(INT_TYPE,name,&iValue);
		}
		else if(!strcmp(type,"str")){
			if(setValue == NULL)
				goto SHOW_HELP;
			Tshm_set(STR_TYPE,name,setValue);
		}
	}
	else if(!strcmp(item,"get")){
		if(type == NULL)
			goto SHOW_HELP;
		
		if(!strcmp(type,"char")){
			if(Tshm_get(CHAR_TYPE,name,&cValue)>0)
				printf("%s=%d\n",name,cValue);
		}
		else if(!strcmp(type,"int")){
			if(Tshm_get(INT_TYPE,name,&iValue)>0)
				printf("%s=%d\n",name,iValue);
		}
		else if(!strcmp(type,"str")){
			if(Tshm_get(STR_TYPE,name,sValue)>0)
				printf("%s=%s\n",name,sValue);
		}
	}
	else if(!strcmp(item,"-h")){
		show_help();
	}
	else
		printf("truedano : Bye,stupid!\n");
	
	return 1;
SHOW_HELP:
	show_help();
	return -1;
}
